package org.inspiration.dudu.web.controller.content;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.inspiration.dudu.common.pojo.Result;
import org.inspiration.dudu.content.model.dto.CommentDTO;
import org.inspiration.dudu.content.model.vo.CommentVO;

import org.inspiration.dudu.web.annotation.Loggable;
import org.inspiration.dudu.web.service.content.CommentFacadeService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comments")
@Tag(name = "评论接口", description = "提供与评论相关的操作接口")
@RequiredArgsConstructor
@Loggable

public class CommentController {

    private final CommentFacadeService commentFacadeService;

    @PostMapping
    @Operation(summary = "上传评论", description = "接收评论数据并返回评论的唯一标识符")
    public Result<String> uploadComment(@RequestBody CommentDTO commentDTO) {
        String uuid = commentFacadeService.uploadComment(commentDTO);
        // 返回评论的唯一标识符
        return Result.success(uuid);
    }

    @Operation(summary = "根据父id查询评论（升序）", description = "根据父评论ID以升序分页获取评论")
    @GetMapping("/asc/{parentId}")
    public Result<List<CommentVO>> getCommentsByParentIdOrderByASC(@PathVariable("parentId") String parentId,
                                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        List<CommentVO> commentVOS = commentFacadeService.getCommentsByParentIdOrderByASC(parentId, pageNum, pageSize);
        return Result.success(commentVOS);
    }

    @Operation(summary = "根据父id查询评论（降序）", description = "根据父评论ID以降序分页获取评论")
    @GetMapping("/desc/{parentId}")
    public Result<List<CommentVO>> getCommentsByParentIdOrderByDESC(@PathVariable("parentId") String parentId,
                                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        List<CommentVO> commentVOS = commentFacadeService.getCommentsByParentIdOrderByDESC(parentId, pageNum, pageSize);
        return Result.success(commentVOS);
    }


    @PutMapping("/mark-as-read")
    @Operation(summary = "标记评论为已读", description = "根据评论ID将评论标记为已读")
    public Result<Void> markAsRead(@RequestBody CommentDTO commentDTO) {
        commentFacadeService.markAsRead(commentDTO);
        return Result.success();
    }

    @GetMapping("/receive")
    @Operation(summary = "获取用户最近收到的评论")
    public Result<List<Map<String, Object>>> getReceiveCommentsByUserIdOrderByDESC(
            @RequestParam("userId") Long userId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize) {
        List<Map<String, Object>> list = commentFacadeService.getReceiveCommentsByUserIdOrderByDESC(userId, pageNum, pageSize);
        return Result.success(list);
    }
}
